package pe.gob.inei.sirtod.repository.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pe.gob.inei.sirtod.domain.DatoAnual;
import pe.gob.inei.sirtod.repository.DatoAnualDAO;

@Repository(value = "datoAnualDAO")
public class SIRTODDatoAnual implements DatoAnualDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@Transactional(readOnly = true)
	public List<DatoAnual> findAllDepartamental(List<String> lisDepSel,
			List<String> lisIndSel, List<String> lisAniSel) {
		return findAll("DatoAnual.findAllDepartamental", lisDepSel, lisIndSel, lisAniSel);
	}

	@Transactional(readOnly = true)
	public List<DatoAnual> findAllProvincial(List<String> lisDepSel,
			List<String> lisIndSel, List<String> lisAniSel) {
		return findAll("DatoAnual.findAllProvincial", lisDepSel, lisIndSel, lisAniSel);
	}

	@Transactional(readOnly = true)
	public List<DatoAnual> findAllDistrital(List<String> lisDepSel,
			List<String> lisIndSel, List<String> lisAniSel) {
		return findAll("DatoAnual.findAllDistrital", lisDepSel, lisIndSel, lisAniSel);
	}
	
	@SuppressWarnings("unchecked")
	private List<DatoAnual> findAll(String sQuery, List<String> lisDepSel,
			List<String> lisIndSel, List<String> lisAniSel) {
		String txtAniIni = lisAniSel.get(0);
		String txtAniFin = lisAniSel.get(lisAniSel.size() - 1);
		Query query = sessionFactory.getCurrentSession().getNamedQuery(
				sQuery);
		query.setParameterList("lisDepSel", lisDepSel);
		query.setParameterList("lisIndSel", lisIndSel);
		query.setString("txtAniIni", txtAniIni);
		query.setString("txtAniFin", txtAniFin);
		return query.list();
	}
}
